﻿namespace GuiaTest
{
    using System;
    using System.Globalization;
    using System.Windows.Automation;
    using Guia.Core;
    using Guia.Elements;
    using Guia.Locator;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using WpfTestApplication;

    /// <summary>
    /// Summary description for AutomationElementFinderTest
    /// </summary>
    [TestClass]
    public class AutomationElementFinderTest : AbstractWindowTest
    {
        [TestMethod]
        public void TestFindFirstButton()
        {
            AutomationElement buttonElement = AutomationElementFinder.FindFirst(window, SearchCondition.ByControlType(ControlType.Button));
            Assert.IsNotNull(buttonElement);
            Assert.IsTrue(buttonElement.Current.Name.StartsWith("Minimi"));
        }

        [TestMethod]
        public void TestFindFirstLabel()
        {
            AutomationElement labelElement = AutomationElementFinder.FindFirst(window, SearchCondition.ByControlType(ControlType.Text));
            Assert.IsNotNull(labelElement);
            Assert.AreEqual("Label 1", labelElement.Current.Name);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestFindFirstWithParentNull()
        {
            AutomationElement labelElement = AutomationElementFinder.FindFirst(null, SearchCondition.ByControlType(ControlType.Text));
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestFindFirstWithSearchConditionNull()
        {
            AutomationElement labelElement = AutomationElementFinder.FindFirst(window, null);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestFindAllWithParentNull()
        {
            AutomationElementCollection labelElements = AutomationElementFinder.FindAll(null, SearchCondition.ByControlType(ControlType.Text));
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestFindAllWithParentNullWithoutSearchCondition()
        {
            AutomationElementCollection labelElements = AutomationElementFinder.FindAll(null);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestFindAllWithSearchConditionNull()
        {
            AutomationElementCollection labelElements = AutomationElementFinder.FindAll(window, null);
        }

        [TestMethod]
        public void TestFindAllLabels()
        {
            AutomationElementCollection labelCollection = AutomationElementFinder.FindAll(window, SearchCondition.ByControlType(ControlType.Text));
            Assert.AreEqual(3, labelCollection.Count);
            AutomationElement labelElement1 = labelCollection[0];
            Assert.AreEqual("Label 1", labelElement1.Current.Name);
            AutomationElement labelElement2 = labelCollection[1];
            Assert.AreEqual("Label 2", labelElement2.Current.Name);
            AutomationElement labelElement3 = labelCollection[2];
            Assert.AreEqual("Label 3", labelElement3.Current.Name);
        }

        [TestMethod]
        public void TestFindAllLabelAndCondition()
        {
            SearchCondition condition =
                SearchCondition.ByControlType(ControlType.Text).And(SearchCondition.ByName("Label 2"));
            AutomationElementCollection labelCollection = AutomationElementFinder.FindAll(window, condition);
            Assert.AreEqual(1, labelCollection.Count);
            AutomationElement labelElement = labelCollection[0];
            Assert.AreEqual("label2", labelElement.Current.AutomationId);
            Assert.AreEqual("Label 2", labelElement.Current.Name);
        }

        [TestMethod, ExpectedException(typeof(ElementNotFoundException))]
        public void TestFindFirstTimeout()
        {
            AutomationElementFinder.FindFirst(window, SearchCondition.ByAutomationId("ThisAutomationIdDoesntExist"));
        }

        [TestMethod, ExpectedException(typeof(ElementNotFoundException))]
        public void TestFindAllTimeout()
        {
            AutomationElement labelElement = AutomationElementFinder.FindFirst(window, SearchCondition.ByAutomationId("label1"));
            Label label = new Label(labelElement);
            AutomationElementFinder.FindAll(label);
        }

        [TestMethod, ExpectedException(typeof(ElementNotFoundException))]
        public void TestFindAllWithSearchConditionTimeout()
        {
            AutomationElementFinder.FindAll(window,SearchCondition.ByControlType(ControlType.DataGrid));
        }

        protected override Type TestWindowType()
        {
            return typeof(AutomationElementFinderTestWindow);
        }

        protected override string TestWindowName()
        {
            return "AutomationElementFinderTestWindow";
        }
    }
}
